﻿// mainFormData
// Created 11/21/2009 11:25:27 AM by Andrzej Prazmo.
// andrzej@prazmo.pl
// Copyright (C) 2009. All rights reserved.

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.IO;

namespace ImageScale
{
    public class mainFormData
    {
        public mainFormData()
        {
            _images = new List<ImageInfo>();
            _percentageScale = 50;
            _absoluteWidth = 800;
            _absoluteHeight = 600;
            _scaleType = ScaleType.Percentage;
            _scaleDestination = ScaleDestination.SaveAs;
        }

        private ScaleType _scaleType;
        public event EventHandler ScaleTypeChanged;
        public ScaleType ScaleType
        {
            get { return _scaleType; }
            set 
            { 
                _scaleType = value;
                if (ScaleTypeChanged != null)
                {
                    ScaleTypeChanged(this, new EventArgs());
                }
                _images.ForEach(delegate(ImageInfo ii) { ii.FireEvents(); });
            }
        }

        private ScaleDestination _scaleDestination;
        public event EventHandler ScaleDestinationChanged;

        public ScaleDestination ScaleDestination
        {
            get { return _scaleDestination; }
            set 
            { 
                _scaleDestination = value;
                if (ScaleDestinationChanged != null)
                {
                    ScaleDestinationChanged(this, new EventArgs());
                }
            }
        }

        private int _percentageScale;
        public int PercentageScale
        {
            get { return _percentageScale; }
            set 
            { 
                _percentageScale = value;
                _images.ForEach(delegate(ImageInfo ii) { ii.FireEvents(); });
            }
        }

        private int _absoluteWidth;
        public int AbsoluteWidth
        {
            get { return _absoluteWidth; }
            set 
            { 
                _absoluteWidth = value;
                _images.ForEach(delegate(ImageInfo ii) { ii.FireEvents(); });
            }
        }

        private int _absoluteHeight;
        public int AbsoluteHeight
        {
            get { return _absoluteHeight; }
            set 
            { 
                _absoluteHeight = value;
                _images.ForEach(delegate(ImageInfo ii) { ii.FireEvents(); });
            }
        }

        private string _selectedFolder;
        public string SelectedFolder
        {
            get { return _selectedFolder; }
            set { _selectedFolder = value; }
        }

        private string _ftpSite;
        public event EventHandler FtpSiteChanged;
        public string FtpSite
        {
            get { return _ftpSite; }
            set 
            { 
                _ftpSite = value;
                if (FtpSiteChanged != null)
                {
                    FtpSiteChanged(this, new EventArgs());
                }
            }
        }
        private string _ftpUser;
        public string FtpUser
        {
            get { return _ftpUser; }
            set { _ftpUser = value; }
        }
        private string _ftpPass;
        public string FtpPass
        {
            get { return _ftpPass; }
            set { _ftpPass = value; }
        }

        private List<ImageInfo> _images;

        public List<ImageInfo> Images
        {

            get { return _images; }

            set { _images = value; }

        }


    }

    public class ImageInfo
    {
        #region Properties...
        private Image _picture;
        public Image Picture
        {
            get { return _picture; }
            set { _picture = value; }
        }

        private Image _thumb;
        public Image Thumb
        {
            get { return _thumb; }
            set { _thumb = value; }
        }

        private string _imagePath;
        public string ImagePath
        {
            get { return _imagePath; }
            set { _imagePath = value; }
        }

        private string _imageType;
        public string ImageType
        {
            get { return _imageType; }
            set { _imageType = value; }
        }

        private string _imageSize;
        public string ImageSize
        {
            get { return _imageSize; }
            set { _imageSize = value; }
        }
        private string _imageFileSize;
        public string ImageFileSize
        {
            get { return _imageFileSize; }
            set { _imageFileSize = value; }
        }
        private string _imageDate;
        public string ImageDate
        {
            get { return _imageDate; }
            set { _imageDate = value; }
        }

        public event EventHandler DestSizeChanged;
        public string DestSize
        {
            get 
            {
                switch (_owner.ScaleType)
                {
                    case ScaleType.Percentage:
                        return String.Format(@"{0} x {1} px", Picture.Width * _owner.PercentageScale / 100,
                            Picture.Height * _owner.PercentageScale / 100);
                    case ScaleType.Absolute:
                        return String.Format(@"{0} x {1} px", _owner.AbsoluteWidth, _owner.AbsoluteHeight);
                }
                return String.Empty;
            }
        }

        public event EventHandler DestFileSizeChanged;
        public string DestFileSize
        {
            get 
            {
                long size = 0;
                switch (_owner.ScaleType)
                {
                    case ScaleType.Percentage:
                        size = ScaleClass.GetImageFileSize(this.Picture, _owner.PercentageScale);
                        break;
                    case ScaleType.Absolute:
                        size = ScaleClass.GetImageFileSize(this.Picture, _owner.AbsoluteWidth, _owner.AbsoluteHeight);
                        break;
                }
                return String.Format("{0:D} KB", size / 1024); ;
            }
        }

        public string SaveAsFileName
        {
            get 
            {
                string sizeString = String.Empty;
                switch (_owner.ScaleType)
                {
                    case ScaleType.Percentage:
                        sizeString = String.Format(@"{0}x{1}", Picture.Width * _owner.PercentageScale / 100, 
                            Picture.Height * _owner.PercentageScale / 100);
                        break;
                    case ScaleType.Absolute:
                        sizeString = String.Format(@"{0}x{1}", _owner.AbsoluteWidth, _owner.AbsoluteHeight);
                        break;
                }
                return String.Format(@"{0}[{1}].{2}", Path.GetFileNameWithoutExtension(this.ImagePath),
                    sizeString, this.ImageType);
            }
        }
        #endregion

        private mainFormData _owner;

        public ImageInfo() { }
        public ImageInfo(string imagePath, mainFormData owner)
        {
            _owner = owner;
            if (File.Exists(imagePath))
            {
                FileInfo imageInfo = new FileInfo(imagePath);
                _imagePath = imagePath;
                _picture = Image.FromFile(imagePath);
                _thumb = MakeThumb(_picture);
                _imageType = imageInfo.Extension.TrimStart(new char[] { '.' });
                _imageSize = String.Format(@"{0} x {1} px", _picture.Width, _picture.Height);
                _imageFileSize = String.Format("{0:D} KB", imageInfo.Length / 1024);
                _imageDate = imageInfo.CreationTime.ToString("d") + " " + imageInfo.CreationTime.ToString("t");
            }
        }
        public void FireEvents()
        {
            if (DestSizeChanged != null)
            {
                DestSizeChanged(this, new EventArgs());
            }
            if (DestFileSizeChanged != null)
            {
                DestFileSizeChanged(this, new EventArgs());
            }
        }
        private Image MakeThumb(Image original)
        {
            int width = 313;
            int height = 211;

            if (original.Height > original.Width)
            {
                float w = (float)original.Width / original.Height;
                width = (int)(height * w);
            }
            ScaleClass sc = new ScaleClass(width, height);
            return sc.Scale(original);
        }
    }

    public enum ScaleDestination
    {
        SaveAs, Clipboard, Overwrite, Folder, Ftp
    }
    public enum ScaleType
    {
        Percentage, Absolute
    }

}
